package com.inspur_video.inspur_video.controller;

import com.inspur_video.inspur_video.bean.Response;
import com.inspur_video.inspur_video.bean.User;
import com.inspur_video.inspur_video.dao.UserDao;
import com.inspur_video.inspur_video.service.UserService;
import com.inspur_video.inspur_video.service.VipService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


/**
 * Created with IntelliJ IDEA.
 *
 * @author 小浩
 * @date 2018-07-25 15:30
 * @package com.inspur_video.inspur_video.controller
 * @description 用户控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    HttpSession httpSession;
    @Autowired
    private UserService userService;
    @Autowired
    private UserDao userDao;
    @Autowired
    private VipService vipService;

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 用户注册
     *
     * @param uName
     * @param uPassword
     * @param uPhoneNumber
     * @param uSex
     * @return
     */
    @RequestMapping(value = "/add")
    public User userAdd(@RequestParam("uName") String uName,
                            @RequestParam("uPassword") String uPassword,
                            @RequestParam("uPhoneNumber") String uPhoneNumber,
                            @RequestParam("uSex") int uSex) {
        Response response = new Response();
        if (userService.findOneByPhoneNumber(uPhoneNumber) == null) {
            //一般在这手机号验证，通过验证则继续，暂时别考虑了
            User user = new User();
            user.setUName(uName);
            user.setUPassword(uPassword);
            user.setUPhoneNumber(uPhoneNumber);
            user.setUSex(uSex);
            response.setCode(200);
            response.setMsg("注册成功");
            return userService.userAdd(user);
        }
        return null;
    }

    /**
     * 手机号登录 返回值200 不是会员
     * 201  是会员
     *
     * @param uPhoneNumber
     * @param uPassword
     * @return
     */
    @RequestMapping(value = "/login")
    public User loginByPhone(String uPhoneNumber, String uPassword) {
        User user = userService.findOneByPhoneNumber(uPhoneNumber);
        if (user != null) {
            String uPS = user.getUPassword();
            if (uPS.equals(uPassword)) {
                System.out.println(httpSession.getId());
                httpSession.setAttribute("user", user.getUId());
                System.out.println("session:"+httpSession.getAttribute("user"));
                if (vipService.isVip(user.getUId())) {
                    user.setMember(1);
                } else {
                    user.setMember(0);
                }
                return user;
            } else {
                return null;
            }
        }
        return null;
    }

    /**
     * 用户名登录 返回会员
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/username_login")
    public Response loginByUserName(String username, String password) {
        logger.warn(username);
        logger.warn(password);
        User user = userService.findOneByUserName(username);
        Response response = new Response();
        System.out.println(user);
        if (user != null) {
            String uPS = user.getUPassword();
            if (uPS.equals(password)) {
                httpSession.setAttribute("user", user.getUId());
                System.out.println(user.getUId());
                httpSession.setAttribute("username", user.getUName());
                if (vipService.isVip(user.getUId())) {
                    response.setCode(201);
                } else {
                    response.setCode(200);
                }
                response.setMsg("登录成功！");
            } else {
                response.setCode(500);
                response.setMsg("密码错误！");
            }
        } else {
            response.setCode(500);
            response.setMsg("用户名不正确");
        }
        return response;
    }


    /**
     * 更新用户信息
     *
     * @param uPhoneNumber
     * @param uName
     * @param uPassword
     * @param uSex
     */
    @RequestMapping(value = "/update")
    public Response userUpdate(String uName,
                               String uPassword,
                               String uPhoneNumber,
                               Integer uSex) {
        User user = new User();
        user.setUId(Integer.parseInt(httpSession.getAttribute("user").toString()));
        user.setUName(uName);
        user.setUPassword(uPassword);
        user.setUPhoneNumber(uPhoneNumber);
        user.setUSex(uSex);
        Response response = new Response();
        if (userService.userAdd(user) != null) {
            response.setCode(200);
            response.setMsg("更新成功！");
        } else {
            response.setCode(500);
            response.setMsg("更新失败");
        }
        return response;
    }

    /**
     * 根据手机号查询用户
     *
     * @param uPhoneNumber
     * @return
     */
    @RequestMapping(value = "/by_phone")
    public User findOneByPhoneNumber(String uPhoneNumber) {
        User user = userService.findOneByPhoneNumber(uPhoneNumber);
        user.setUPassword("");
        return user;
    }

    /**
     * 获取当前用户信息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/current")
    public User findOne() {
        User user = userService.findOneById(Integer.parseInt(
                httpSession.getAttribute("user").toString()));
        return user;
    }

}
